package es.udc.pfc.davidparedes.web.pages.user;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;

import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;
import es.udc.pfc.davidparedes.model.userprofile.UserProfile;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;
import es.udc.pfc.davidparedes.model.vendor.Vendor;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.MANAGER_OR_ABOVE)
public class UpdateVendor {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Property
    private Vendor vendor;
    
    @Property
    private Long updateVendorId;
    
    @Property
    private Long removeVendorId;

    @Property
    private String firstName;

    @Property
    private String lastName;

    @Property
    private String email;
    
    @Property
    private String nif;
    
    @Property
    private int baseHours;
    
    @Property
    private Date contractTerminationDate;
    
    @Component
    private Form updateVendorForm;
    
    @Component
    private Form removeVendorForm;
    
    @Inject 
    private PageRenderLinkSource pageLinkSource;
    
    
    void onActivate(Long id) {
    	try {
       		
    		vendor = userService.findVendor(id);
    		updateVendorId = removeVendorId = id;
    		
    		firstName = vendor.getFirstName();
    		lastName = vendor.getLastName();
    		email = vendor.getEmail();
    		nif = vendor.getNif();
    		baseHours = vendor.getBaseHours();
    		contractTerminationDate = vendor.getContractTermination().getTime();
    		
			} catch (InstanceNotFoundException e) {
		}
    }

    Object onValidateFromUpdateVendorForm() throws InstanceNotFoundException {
    	
        if (!updateVendorForm.isValid()) {
            return this;
        }
        
    	Calendar contractTermination = Calendar.getInstance();
    	contractTermination.setTime(contractTerminationDate);
    
		userService.findUserProfile(updateVendorId);
        userService.updateVendor(updateVendorId, firstName, lastName, email, nif, contractTermination, baseHours);
        
        return pageLinkSource.createPageRenderLinkWithContext("user/DisplayVendorProfile", updateVendorId);
        
    }
    
    Object onValidateFromRemoveVendorForm() throws InstanceNotFoundException {
    	
    	Long universeId = (userService.findVendor(removeVendorId)).getUniverse().getUniverseId();
    	
    	userService.deactivateVendor(removeVendorId);
    	
    	UserProfile user = userService.findUserProfile(userSession.getUserProfileId());
    	if (user instanceof UniverseManager) {
    		return pageLinkSource.createPageRenderLinkWithContext("user/UniverseStaff", universeId);
    	}
    	return UniverseSelection.class;
    }

}
